package se.hb.eme;

import java.lang.reflect.Type;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;


public class EnumJsonDeserializer implements JsonDeserializer {

	private Class type;
	public EnumJsonDeserializer(Class type)
	{
		this.type = type;
	}
	
	@Override
	public Object deserialize(JsonElement json, Type typeOfT,
			JsonDeserializationContext context) throws JsonParseException {
		
		try {
			return Enum.valueOf(type, new JSONObject(json.toString()).getString("name") );
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	
	
	

	
}
